package cn.hnu.qq.client.panel;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import org.apache.log4j.Logger;

import cn.hnu.qq.client.controller.ListenClientMessageThread;
import cn.hnu.qq.client.controller.ListenServerMessageThread;
import cn.hnu.qq.client.controller.SingleChartController;
import cn.hnu.qq.client.event.MessageEvent;
import cn.hnu.qq.client.listener.MessageListener;
import cn.hnu.qq.entity.User;
import cn.hnu.qq.entity.message.ChartMessage;
import cn.hnu.qq.entity.message.Message;
import cn.hnu.qq.global.MessageType;

public class SingleChartPanel extends JPanel implements ActionListener,MessageListener{
	
	private int count = 0;

	private SystemPanel system;
	
	private SingleChartController controller = SingleChartController.getInstance();
	
	private Map<String,Integer> openedPanel = new HashMap<String,Integer>(); 
	
	private JTabbedPane tabbedPane;
	/**
	 * Create the panel.
	 */
	@Override
	protected void paintComponent(Graphics g) {
		Image image = null;
		try {
			image = ImageIO.read(new File("src/main/java/image/07.jpg"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
				
		g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);

	}
	private SingleChartPanel() {
		setLayout(null);
		this.setSize(555, 480);
		
		JPanel panel = new JPanel(){
			@Override
			protected void paintComponent(Graphics g) {
				Image image = null;
				try {
					image = ImageIO.read(new File("src/main/java/image/02.jpg"));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				};
						
				g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);

			}
		};
		panel.setBounds(10, 10, 535, 460);
		add(panel);
		panel.setLayout(null);
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(10, 10, 515, 440);
		panel.add(tabbedPane);
		ListenClientMessageThread.getInstance().addMessageListener(this);
		ListenServerMessageThread.getInstance().addMessageListener(this);
	}
	
	public void createChartTab(User user){
		System.out.println("进入createChartTab");
		
		if(openedPanel.get(user.getAcount()) != null){
			int i = openedPanel.get(user.getAcount());
			tabbedPane.setSelectedIndex(i);
//			controller.getSingDialog().refreshPanel(this);
		}else{
			System.out.println("进入createChartTab else");
			DefaultModelChartPanel dp = new DefaultModelChartPanel();
			tabbedPane.add(dp,count);
			tabbedPane.setTitleAt(count, user.getAcount());
			tabbedPane.setSize(dp.getSize());
			openedPanel.put(user.getAcount(), count);
			
			
			system = new SystemPanel(user.getAcount());
			system.addActionListener(this);
			tabbedPane.setComponentAt(count++, system);
			controller.getSingDialog().refreshPanel(this);
		}
		
//		system = new SystemPanel(user.getAcount());
//		system.addActionListener(this);
//		tabs.setComponentAt(count++, system);

	}

	public void actionPerformed(ActionEvent e) {
		tabbedPane.remove(tabbedPane.getSelectedIndex());
		openedPanel.remove(((SystemPanel)e.getSource()).getTitle());
		count--;
	}

	public void messageArrived(MessageEvent event) {
		Logger.getLogger(SingleChartController.class).debug("SingleChartPanel获取消息 　：" + event);
	}
}
